package Main;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class MovieDay
{

	public boolean hasMovie(String title)
	{
		for (Movie m : movies)
		{
			if (m.title.equals(title))
				return true;
		}
		return false;
	}

	public List<Movie> getNewMovies(MovieDay theOtherDay)
	{
		List<Movie> res = new ArrayList<Movie>();

		for (Movie m : movies)
		{
			if (!theOtherDay.hasMovie(m.title))
			{
				res.add(m);
				System.out.println("New movie: " + m.title);
			}
		}
		return res;

	}

	private List<Movie> movies;

	public MovieDay()
	{
		super();
		setMovies(new ArrayList<Movie>());
	}

	public String toStringHTMLTable()
	{
		String body = "";
		body += "<table cellspacing=\"10\">";
		for (Movie m : getMovies())
		{
			// if (googleImages)
			body += m.ToStringHTMLinTable();
		}
		body += "</table>";

		body += "<br/><strong>" + "Total " + getMovies().size() + " movies</strong><br/>";
		return body;
	}

	public String toStringHTML()
	{
		String res = "";
		for (Movie m : getMovies())
		{
			res += m.toStringHTML() + "<br/>";
		}
		res += "<br/><strong>" + "Total " + getMovies().size() + " movies</strong><br/>";
		return res;
	}

	@Override
	public String toString()
	{
		String res = "";
		for (Movie m : getMovies())
		{
			res += m.toString() + "\n";
		}
		res += "\n" + "Total " + getMovies().size() + " movies";
		return res;
	}

	public String printForExcel()
	{
		String res = "TITLE; DATE; CINEMA CITY; CINEMA NAME; ROOM NUMBER; ROOM CAPACITY\n";
		for (Movie m : getMovies())
		{
			res += m.title + "; " + m.date + "; " + m.cinema.city + ";" + m.cinema.name + "; " + m.room.number + "; " + m.room.capacity + "\n";

		}
		return res;
	}

	public static MovieDay load(String xmlString)
	{
		XStream xstream = new XStream(new DomDriver("UTF-8"));

		return (MovieDay) xstream.fromXML(xmlString);
	}

	public void save()
	{

		// saving XML
		XStream xstream = new XStream(new DomDriver("UTF-8"));
		String xml = xstream.toXML(this);

		// long timestamp = System.currentTimeMillis() / 1000;
		// String filename = timestamp + ".xml";

		System.out.println("Writing cinema xml file for today....");
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
		StringBuilder todayString = new StringBuilder(dateFormatter.format(new Date()));
		String filename = "cinema_" + todayString.toString() + ".xml";
		PrintWriter out;
		try
		{
			out = new PrintWriter(new FileWriter(filename, false));
			out.print(xml);
			out.close();
			System.out.println("Saving " + filename);
		} catch (IOException e)
		{
			System.out.println("MovieDay: Saving error! '" + filename + "'");
		}

		// saving today HTML
		System.out.println("Writing cinema html file for today....");
		filename = "cinema_" + todayString.toString() + ".html";
		try
		{
			// out = new PrintWriter(new FileWriter(filename));
			BufferedWriter out2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename, false), "UTF-8"));
			out2.write(this.toStringHTMLTable());
			out2.close();
			System.out.println("Saving " + filename);
		} catch (IOException e)
		{
			System.out.println("MovieDay: Saving error! '" + filename + "'");
		}

		// //for google images, save also array of current movie names;
		// filename = "currentMovieTitleArray.txt";
		// try {
		// // out = new PrintWriter(new FileWriter(filename));
		// BufferedWriter out2 = new BufferedWriter(new OutputStreamWriter(
		// new FileOutputStream(filename, false), "UTF-8"));
		// out2.write(this.allTitletoString());
		// out2.close();
		// System.out.println("Saving " + filename);
		// } catch (IOException e) {
		// System.out.println("MovieDay: Saving error! '" + filename + "'");
		// e.printStackTrace();
		// }

	}

	private String allTitletoString()
	{
		String res = "\"exlajlkf\"";
		for (Movie m : getMovies())
		{
			res += ",\"" + m.title + " movie poster\"";
		}
		return res;
	}

	public List<Movie> getMovies()
	{
		return movies;
	}

	public void setMovies(List<Movie> movies)
	{
		this.movies = movies;
	}

	public String toMysqlInsert()
	{
		String res = "";
		for (Movie m : getMovies())
		{
			res += m.toMysqlInsert() + "\n";
		}
		return res;
	}

	public String toARFFData()
	{
		String res = "";
		for (Movie m : getMovies())
		{
			res += m.toARFFData() + "\n";
		}
		return res;
	}
}
